﻿namespace TfsExtensions.Build.Controls
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using TfsExtensions.Build.Activities.Entities;
    using System.Collections.ObjectModel;

    public class MetadataConverter : ExpandableObjectConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return (destinationType == typeof(string) || base.CanConvertTo(context, destinationType));
        }


        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (!(destinationType == typeof(string)) || !(value is Collection<AssemblySettings>))
            {
                return base.ConvertTo(context, culture, value, destinationType);
            }

            var metadataCollection = (Collection<AssemblySettings>)value;

            if (metadataCollection != null && metadataCollection.Count > 0)
            {
                StringBuilder builder = new StringBuilder();
                foreach (var metadata in metadataCollection)
                {
                    if(metadata.AssemblyVersionDetails.IsAssemblyVersionSelected)
                    {
                        builder.Append(string.Format("Version: {0}; ", metadata.Version));
                        builder.Append(string.Format("File Version: {0}; ",metadata.FileVersion));
                    }
                    
                    if(metadata.AssemblySigningDetails.IsAssemblySigning || metadata.AssemblySigningDetails.IsDelaySigned)
                    {
                        builder.Append(string.Format("Assembly Signing : {0}; ", metadata.IsSigningEnabled));
                    }
                    
                    if(metadata.AssemblyMetadataDetails.IsAssemblyMetadataSelected)
                    {
                        builder.Append(string.Format("Metadata : {0}; ", metadata.IsMetadataEnabled));
                    }

                    //todo: remove break later
                    break;
                }

                return builder.ToString();
            }

            return string.Empty;
        }
    }
}
